package com.pikaqiu.demo.service.impl;

import com.pikaqiu.demo.entity.message.TextMessage;
import com.pikaqiu.demo.service.WeChatService;
import com.pikaqiu.demo.utils.WeChatMessageUtil;
import com.pikaqiu.demo.utils.WeChatUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;


/**
 * @author 吕梁山
 * @date 2019/4/22
 */
@Service("weChatService")
public class WeChatServiceImpl implements WeChatService {

    private final Logger logger = LoggerFactory.getLogger(WeChatServiceImpl.class);

    @Resource
    private WeChatUtil weChatUtil;

    @Override
    public String returnMessage(Map<String, String> map) throws Exception {
        logger.info("------------微信消息开始处理-------------");
        String fromUserName = map.get("FromUserName");
        TextMessage textMessage = new TextMessage();
        textMessage.setCreateTime(new Date().getTime());
        textMessage.setFromUserName(map.get("ToUserName"));
        textMessage.setToUserName(fromUserName);
        textMessage.setMsgType(WeChatMessageUtil.RESP_MESSAGE_TYPE_TEXT);
        String msgType = map.get("MsgType");
        if(WeChatMessageUtil.REQ_MESSAGE_TYPE_TEXT.equals(msgType)){
            String s = map.get("Content");
            if("创建菜单".equals(s)){
//                weChatUtil.createMenuByText();
            }
            textMessage.setContent("发送的消息是：文本，" + s);
        } else if (WeChatMessageUtil.REQ_MESSAGE_TYPE_LINK.equals(msgType)){
            String s = map.get("Content");
            textMessage.setContent("发送的消息是：链接，" + s);
        } else if (WeChatMessageUtil.REQ_MESSAGE_TYPE_IMAGE.equals(msgType)){
            String s = map.get("PicUrl");
            textMessage.setContent("发送的消息是：图片，" + s);
        } else if (WeChatMessageUtil.REQ_MESSAGE_TYPE_VOICE.equals(msgType)) {
            String s = map.get("MediaId") + "." +  map.get("Format");
            textMessage.setContent("发送的消息是：语音，" + s);
        } else if (WeChatMessageUtil.REQ_MESSAGE_TYPE_LOCATION.equals(msgType)) {
            String s = map.get("Label") + ":" + map.get("Location_X") + ":" + map.get("Location_Y");
            textMessage.setContent("发送的消息是：位置，" + s);
        } else if (WeChatMessageUtil.REQ_MESSAGE_TYPE_EVENT.equals(msgType)) {
            // 监听触发事件
            String event = map.get("Event");
            if(WeChatMessageUtil.EVENT_TYPE_SUBSCRIBE.equals(event)){
                // 新增一条用户关注记录
                textMessage.setContent("触发的事件是：关注");
            } else if (WeChatMessageUtil.EVENT_TYPE_UNSUBSCRIBE.equals(event)){
                // 新增一条用户取消关注记录
                textMessage.setContent("触发的事件是：取消关注");
            } else if (WeChatMessageUtil.EVENT_TYPE_CLICK.equals(event)){
                String eventKey=map.get("EventKey");
                textMessage.setContent("触发的事件是：菜单点击事件，" + eventKey);
            }
        }
        return WeChatMessageUtil.textMessageToXML(textMessage);
    }
}
